package cn.zhaozhiqi.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.zhaozhiqi.constants.Constants;
import cn.zhaozhiqi.mapper.PayOrderMapper;
import cn.zhaozhiqi.pojo.domain.PayOrder;
import cn.zhaozhiqi.pojo.order.dto.PayOrderDto;
import cn.zhaozhiqi.service.IPayOrderService;
import cn.zhaozhiqi.template.GeTuiTemplate;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhaozhiqi
 * @since 2025-01-09
 */
@Service
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {

    @Autowired
    private GeTuiTemplate geTuiTemplate;

    /**
     * 绑定cid和别名（customerId）的关系
     * @param cid
     * @return
     */
    @Override
    public Object bindalias(String cid) {
        String loginIdAsLong = StpUtil.getLoginIdAsString();
        geTuiTemplate.bindAlias(cid,loginIdAsLong);
        return true;
    }

    /**
     * 通过order传递的参数，进行payorder的保存和个推给乘客端
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Boolean createPayOrder(PayOrderDto dto) {
        //1.统一支付单保存
        PayOrder payOrder = new PayOrder();
        payOrder.setCreateTime(new Date());
        payOrder.setPayStatus(Constants.PayStatus.WATTING);
        payOrder.setSubject("123456");
        BeanUtils.copyProperties(dto,payOrder);
        super.save(payOrder);
        //2.个推给乘客消息,将订单号给乘客
        geTuiTemplate.pushToSingleByAlias(payOrder.getOrderNo(),payOrder.getPayUserId().toString());
        return true;
    }

    /**
     * 通过order微服务传入的对象内的orderNo进行payOrder查询
     * @param orderNo
     */
    @Override
    public PayOrder getByOrderNo(String orderNo) {
        return super.getOne(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getOrderNo,orderNo));
    }


}
